package com.stylefeng.guns.modular.system.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.dao.DeptMapper;
import com.stylefeng.guns.common.persistence.model.Dept;
import com.stylefeng.guns.common.persistence.model.Dot;
import com.stylefeng.guns.common.persistence.dao.DotMapper;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.business.service.IDotService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiejie123
 * @since 2018-01-15
 */
@Service
public class DotServiceImpl extends ServiceImpl<DotMapper, Dot> implements IDotService {
    @Resource
    DeptMapper deptMapper;
    @Override
    public Tip insertDot(Dot dot) {
        Dept d=deptMapper.selectById(dot.getDeptid());
        Wrapper<Dot> wrapper_dot = new EntityWrapper<>();
        wrapper_dot = wrapper_dot.where("deptid={0} ", dot.getDeptid());
        int j=this.selectCount(wrapper_dot);

        if(d.getDot_num()<=j){
            return new ErrorTip(Const.FAILUER,"网点数量已达最大值");
        }

        Wrapper<Dot> wrapper = new EntityWrapper<>();
        wrapper = wrapper.where("dot_name={0} ", dot.getDotName()).andNew("deptid={0}",dot.getDeptid());
        int i=this.selectCount(wrapper);
        if(i>0){
            return new ErrorTip(Const.FAILUER,"该商户下已存在相同网点名称，请重新填写");
        }
        this.insert(dot);
        if(dot.getId()==null){
            return new ErrorTip(Const.FAILUER,"操作失败");
        }else{
            return new SuccessTip();
        }
    }

    @Override
    public Tip updateDot(Dot dot) {
        Wrapper<Dot> wrapper = new EntityWrapper<>();
        wrapper = wrapper.where("dot_name={0} ", dot.getDotName())
                .andNew("deptid={0}", ShiroKit.getUser().getDeptId()).andNew("id !={0}",dot.getId());
        int i=this.selectCount(wrapper);
        if(i>0){
            return new ErrorTip(Const.FAILUER,"该商户下已存在相同网点名称，请重新填写");
        }
        if(this.updateById(dot)){
            return new SuccessTip();
        }else{
            return new ErrorTip(Const.FAILUER,"操作失败");
        }
    }
}
